import torch

''' pytorch configuration variables '''
#input data & target label(groundturth)
train_x = torch.tensor([[0.05, 0.10]])
train_y = torch.tensor([[0.01, 0.99]])

# layer 1 & 2 parameters
layer1_w = torch.Tensor([[0.15, 0.25], [0.20, 0.30]])
layer1_b = torch.Tensor([[0.35]])
layer2_w = torch.Tensor([[0.40, 0.50], [0.45, 0.55]])
layer2_b = torch.Tensor([[0.60]])

lr = 0.5
for _iter in range(10000):
    layer1_w = torch.autograd.Variable(layer1_w, requires_grad=True)
    layer2_w = torch.autograd.Variable(layer2_w, requires_grad=True)

    ''' forward '''
    layer1_output = (train_x.mm(layer1_w) + layer1_b).sigmoid()
    layer2_output = (layer1_output.mm(layer2_w) + layer2_b).sigmoid()
    loss_total = (layer2_output - train_y).pow(2).sum() / 2
       

    ''' backward '''
    loss_total.backward()

    ''' update '''
    with torch.no_grad():
        layer1_w = layer1_w - lr * layer1_w.grad
        layer2_w = layer2_w - lr * layer2_w.grad
        # print('layer1_w', layer1_w)
        # print('layer2_w', layer2_w)

    if _iter % 100 == 0:
        print("total loss:", loss_total)
